package com.mc.jf.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 计划小程序_用户
 * </p>
 *
 * @author Lin
 * @since 2023-05-03
 */
@Data
@TableName("plan_user")
public class PlanUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;
    @TableId(type = IdType.AUTO)
    private String id;

    private String username;

    private String unit;
    private String pwd;

    @TableField(exist = false)
    private int role;
    @TableField(exist = false)
    public static int roleAdmin = 999;
    @TableField(exist = false)
    public static int rolePlan = 1;

    @TableField(exist = false)
    List<String> permission;

    @JSONField(serialize = false)
    @TableField(exist = false)
    @JsonIgnore
    List<SimpleGrantedAuthority> authorities;

    public PlanUser() {
    }

    public PlanUser(String id, String username, String pwd, String unit, int role, List<String> permission) {
        this.id = id;
        this.username = username;
        this.pwd = pwd;
        this.role = role;
        this.unit = unit;
        this.permission = permission;
    }

    @Override
    public String toString() {
        return "PlanUser{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", unit='" + unit + '\'' +
                ", pwd='" + pwd + '\'' +
                ", role=" + role +
                ", permission=" + permission +
                ", authorities=" + authorities +
                '}';
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) {
            return authorities;
        }
        authorities = permission.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());

        return authorities;
    }

    @Override
    public String getPassword() {
        return pwd;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public boolean isAdmin() {
        return role == roleAdmin;
    }


}
